/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkUtil/MkLog.h"
#include "MkUtil/MkUtil.h"
#include <gtest/gtest.h>

Uint32 TestCheckError(Uint32 ErrorCode)
{
    MkCheckErrorLog(ErrorCode, "none\n");
    return NoneError;
}

TEST(MkLog, InitTest)
{
    MkLogInit("log_test");
    MkLogChangeWriteDst(TO_FILE|TO_SCREEN);
    MkLogInitLevel("log_test", TO_FILE | TO_SCREEN);
    MkLogChangeLevel(LOG_DEBUG);

    MkLogLevel Level;
    Uint32 ErrorCode = MkLogGetLevel(Level);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_EQ(Level, LOG_DEBUG);

    Uint32 Dst;
    ErrorCode = MkLogGetWriteDst(Dst);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_EQ(TO_FILE | TO_SCREEN, Dst);

    MkString LogStr;
    CMkUtil::CreateRandomString(Len2K, LogStr);
    for (int i = 0; i < 10; i++) {
        MkDebugLog("test log:%s\n", LogStr.c_str());
    }

    MkString ModuleName;
    ErrorCode = MkLogGetModuleName(ModuleName);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_STREQ("log_test", ModuleName.c_str());

    TestCheckError(NoneError);
    TestCheckError(CreateFolderFailed);

    MkErrorLog("test error\n");
    MkInfoLog("test info\n");
    MkWarningLog("test warn\n");
    MkDebugLog("test debug\n");
    MkLogChangeLevel(LOG_MAX);  //reset to log max
    MkLogChangeLevel(LOG_DEBUG);
}

